package com.yunxi.core.util;


import java.text.DecimalFormat;

public class WgrNumber
{
    public static final int ADD_BACK = 1;

    public static final int ADD_FORE = 2;

    public static String format(float s, String format)
    {
        String str = "";

        DecimalFormat df = new DecimalFormat(format);

        str = df.format(s);
        if (str.equals(format))

        str = "&nbsp;";

        return str;
    }

    public static String setChar(char chr, int totalLen)
    {
        return setChar("", chr, totalLen, 2);
    }

    public static String setChar(String str, char chr, int totalLen)
    {
        /* 59 */return setChar(str, chr, totalLen, 2);
    }

    public static String setChar(String str, char chr, int totalLen, int position)
    {
        if ((totalLen < 0) || (str == null)) return str;
        int sLen = str.length();
        String ret = str;
        for (int i = 0; i < totalLen - sLen; ++i)
        {
            if (position == 1)
            {
                ret = ret + chr;
            }
            else
                ret = chr + ret;
        }

        return ret;
    }

    public static String getNext(String curr, int len)
    {
        if (curr == null) return setChar('0', len);
        String ret = "";
        long lValue = new Long(curr).longValue() + 1L;
        ret = ret + lValue;
        ret = setChar('0', len - ret.length()) + ret;
        return ret;
    }

    public static String valiNumber(String numberstr)
    {
        if (numberstr.matches("\\p{Digit}+\\.?+\\p{Digit}{0,2}"))
        {
            return numberstr;
        }
        return "0";
    }

    public static boolean isHasNumber(String numberstr)
    {
        String[] str = numberstr
                .split("\\p{Digit}+\\.?+\\p{Digit}{0,2}");

        return (str.length == 0);
    }

    public boolean checkNum(String s)
    {
        try
        {
            float f = Float.valueOf(s).floatValue();

            if (f < 0.0F)
            {

                return false;
            }

            return true;
        } catch (NumberFormatException e)
        {
        }
        return false;
    }

    public String roundString(String s)
    {
        if ("".equals(s))
        {
            return "";
        }

        double d = Double.parseDouble(s);

        d = (d * 100.0D + 0.5D) / 100.0D;

        s = new DecimalFormat("##0.000").format(d);

        int index = s.indexOf(".");

        String intOnly = s.substring(0, index);

        if (intOnly.length() > 13)
        {

            return "";
        }

        String smallOnly = s.substring(index + 1);

        if (smallOnly.length() > 2)
        {
            String roundSmall = smallOnly.substring(0, 2);

            s = intOnly + "." + roundSmall;
        }

        return s;
    }

    /**
     * id
     */
    private static int idSequence = 10000;

    public synchronized static String generalSrid()
    {
        StringBuffer ret = new StringBuffer(20);
        ret.append(WgrDate.getCurrentTime("yyyyMMddHHmmss"));
        idSequence++;
        if (idSequence > 20000) idSequence -= 10000;
        ret.append(String.valueOf(idSequence).substring(1));
        return ret.toString();
    }

    public static Float filter(Float a){
        if(a==null)
            return 0.0f; 
        return a;
    }
    
    public static Double filter(Double a){
        if(a==null)
            return 0.0d; 
        return a;
    }
    
    public static Integer filter(Integer a){
        if(a==null)
            return 0; 
        return a;
    }
    
    public static Long filter(Long a){
        if(a==null)
            return 0l; 
        return a;
    }
    
    public static void main(String[] args)
    {
        System.out.println(WgrNumber.getNext("6",5));
    }
}